﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    /// <summary>
    /// 员离职的控制器
    /// </summary>
    public class DimissionController : Controller
    {
        private IEmployeeRepository employeeRepository;
        private IUnitOfWork unitOfWork;

        public DimissionController(IEmployeeRepository employeeRepository,
                                   IUnitOfWork unitOfWork)
        {
            this.employeeRepository = employeeRepository;
            this.unitOfWork = unitOfWork;
        }

        //未离职的Employee转换为BiographyOutline,显示到Telrki的Grid当中
        private IEnumerable<BiographyOutline> EmployeesToBiographyOutline()
        {
            return from employee in employeeRepository.GetMany(e => e.EmployeeStatus != "离职状态").AsQueryable()
                   select new BiographyOutline
                              {
                                  ID = employee.ID,
                                  Name = employee.Name,
                                  Sex = employee.Sex,
                                  Department = employee.Department.Name,
                                  Post = employee.Post.Title,
                                  Degree = employee.Degree,
                                  EmployeeStatus = employee.EmployeeStatus
                              };
        }
        //将Employee的概要数据通过BiographyOutline显示到Grid当中去
        public PartialViewResult Index()
        {
            return PartialView(EmployeesToBiographyOutline());
        }

        //用于分页排序和筛选
        [GridAction]
        public ActionResult TelerikAjax()
        {
            return View(new GridModel(EmployeesToBiographyOutline()));
        }

        //员工离职HttpGet
        [HttpGet]
        public PartialViewResult Dimission(int id)
        {
            Employee employee = employeeRepository.GetById(id);
            DimissionModel dimissionModel;
            if (employee != null)
            {
                dimissionModel = new DimissionModel
                                     {
                                         ID = employee.ID,
                                         Name = employee.Name,
                                         Sex = employee.Sex,

                                         Post = employee.Post.Title,
                                         Department = employee.Department.Name,
                                         JoinTime = employee.JoinTime,
                                         DimissionDate = employee.DimissionDate,
                                         DimissionRemark = employee.DimissionRemark,
                                         Identitycard = employee.Identitycard,
                                     };
            }
            else
            {
                dimissionModel = new DimissionModel();
            }
            return PartialView(dimissionModel);
        }

        //员工离职HttpPost
        [HttpPost]
        public RedirectToRouteResult Dimission(DimissionModel dimissionModel)
        {
            Employee employee = employeeRepository.GetById(dimissionModel.ID);

            //视图模型通过验证
            if (ModelState.IsValid)
            {
                employee.EmployeeStatus = dimissionModel.EmployeeStatus;
                employee.DimissionDate = dimissionModel.DimissionDate;
                employee.DimissionRemark = dimissionModel.DimissionRemark;

                employeeRepository.Update(employee);
                unitOfWork.Commit();

            }

            Session["url"] = "/Dimission/Index";
            return RedirectToAction("Index", "HumanResources");

        }
    }
}
